package org.jtb.quickr;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.Logger;

class SignedUrl extends ServiceUrl {
    private static Logger logger = Logger.getLogger(SignedUrl.class.getName());
    
    private String secret;
    private AuthResponse.Token authToken;
    
    public SignedUrl(String baseUrl, String apiKey, String secret) throws QuickrException {
        this(baseUrl, new HashMap<String,String>(), apiKey, secret, null);
    }
    
    public SignedUrl(String baseUrl, String apiKey, String secret, AuthResponse.Token authToken) throws QuickrException {
        this(baseUrl, new HashMap<String,String>(), apiKey, secret, authToken);
    }
    
    public SignedUrl(String baseUrl, Map<String,String> args, String apiKey, String secret) throws QuickrException {
        this(baseUrl, args, apiKey, secret, null);
    }
    
    public SignedUrl(String baseUrl, Map<String,String> args, String apiKey, String secret, AuthResponse.Token authToken) throws QuickrException {
        super(baseUrl, args, apiKey);
        this.secret = secret;
        this.authToken = authToken;
    }
    
    protected Map<String,String> getArgs() {
        if (authToken != null) {
            putArg("auth_token", authToken.toString());
        }
        
        if (secret != null) {
            ApiSignature apiSig = new ApiSignature(secret, super.getArgs());
            String sig = apiSig.getSignature();
            putArg("api_sig", sig);
        }
        
        return super.getArgs();
    }
}
